package com.eims.sls_app.ui.adapter;

import java.util.List;

import com.eims.sls_app.view.MyGridView;
import com.eims.sls_app.view.MyToast;

import bls.juejin.cn.R;



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AvatarLvAdapter extends BaseAdapter {
	private String[] title;
	private List iconArray;
	private Context context;
	private int pos;
	private int selectParentIndex;// 父id
	private int selectItemIndex;// 子id


	public AvatarLvAdapter(String[] title, List iconArray, Context context) {
		super();
		this.title = title;
		this.iconArray = iconArray;
		this.context = context;
	}

	@Override
	public int getCount() {
		
		return iconArray.size();
	}

	@Override
	public Object getItem(int position) {
		
		return position;
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHoloder holoder = null;
		if (convertView==null) {
			holoder = new ViewHoloder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.layout_avatar_gv_item, null);
			holoder.tv_title = (TextView) convertView.findViewById(R.id.tv_title_gv);
			holoder.gv_avatar = (MyGridView) convertView.findViewById(R.id.gv_avatar);
			convertView.setTag(holoder);
		}else {
			holoder = (ViewHoloder) convertView.getTag();
		}
		pos = position;
		holoder.tv_title.setText(title[position]);
		MyGridViewAvatarAdapter adapter = new MyGridViewAvatarAdapter((int[]) iconArray.get(position),position);
		holoder.gv_avatar.setAdapter(adapter);
		holoder.gv_avatar.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				//MyToast.getToast(context, "position="+position+"pos="+pos).show();
				if (null != listener) {
					listener.getIndex(selectParentIndex, selectItemIndex);
				}
			}
		});
		
		return convertView;
	}
	
	public class ViewHoloder {

		TextView tv_title;
		MyGridView gv_avatar;
	}
	
	public void setSelectIndex(int selectParentIndex, int selectItemIndex) {
		this.selectParentIndex = selectParentIndex;
		this.selectItemIndex = selectItemIndex;
	}
	
	class MyGridViewAvatarAdapter extends BaseAdapter{

		private int[] pic;
		private int pos2;
		

		public MyGridViewAvatarAdapter(int[] pic,int position) {
			super();
			this.pic = pic;
			this.pos2 = position;
		}

		@Override
		public int getCount() {
			
			return pic.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHoloder holoder = null;
			if (convertView==null) {
				holoder = new ViewHoloder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.layout_img_gv_item, null);
				holoder.ic_icon = (ImageView) convertView.findViewById(R.id.iv_head);
				
				convertView.setTag(holoder);
			}else {
				holoder = (ViewHoloder) convertView.getTag();
			}
			holoder.ic_icon.setImageResource(pic[position]);
			if (selectItemIndex == position && selectParentIndex == pos2) {
				holoder.ic_icon.setBackgroundResource(R.drawable.btn_playtype_select_86);// 设置选中状态
			}
			holoder.ic_icon.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					//holoder.ic_icon.setBackgroundResource(R.drawable.btn_playtype_select_86);
					//holoder.ic_icon.setBackgroundDrawable();
					MyToast.getToast(context, "position="+position+"pos="+pos2).show();
				}
			});
			
			return convertView;
		}
		public class ViewHoloder {
			ImageView ic_select;
			ImageView ic_icon;
		}
		
		
	}
	
	private OnSelectedListener listener;

	public void setOnSelectedListener(OnSelectedListener listener) {
		this.listener = listener;
	}

	public interface OnSelectedListener {
		void getIndex(int parentIndex, int itemIndex);
	}
	
	

}
